//
//  StoreServerEditViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/6/4.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "StoreServerEditViewController.h"
#import "LineItemCell.h"
#import "LineActionItemCell.h"
#import "AddStoreServerViewController.h"

@interface StoreServerEditViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) NSMutableArray *listArr;
@end

@implementation StoreServerEditViewController
-(void)rightAction:(UIButton *)sender
{
    AddStoreServerViewController * addVC = mGetStoryboardViewController(@"Main", @"addStoreServerViewController");
    NavigationController_pushVC(addVC);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"店铺服务");
    [self customNavigationItems];
    [self addRightBarButtonItemWithTitle:@"新增" action:@selector(rightAction:)];
    self.listArr = [[NSMutableArray alloc]init];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1+self.listArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LineItemCell * itemCell = mGetTableViewCell(@"lineItemCell");
        return itemCell;
    }else{
        LineActionItemCell * itemCell = mGetTableViewCell(@"lineActionItemCell");
        NSDictionary * infoDic = self.listArr[indexPath.row -1];
        itemCell.nameLabel.text = infoDic[@"project"];
        itemCell.priceLabel.text = [NSString stringWithFormat:@"¥%@",infoDic[@"price"]];
        itemCell.toPriceLabel.text = [NSString stringWithFormat:@"¥%@",infoDic[@"servicePrice"]];
        itemCell.returnBtnActionBlock = ^(NSInteger type) {
            if (type == 0) {//编辑
                AddStoreServerViewController * addVC = mGetStoryboardViewController(@"Main", @"addStoreServerViewController");
                addVC.infoDic = infoDic;
                NavigationController_pushVC(addVC);
            }else{//删除
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"摩嗒嗒提醒您" message:@"是否删除该服务,删除后不可恢复" preferredStyle: UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //点击按钮的响应事件；
                    
                }]];
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
                    setDickeyobj(requestInfo, infoDic[@"serviceId"], @"serviceId");
                    setDickeyobj(requestInfo, @"1", @"add");
                    [PPNetworkHelper POST:app_person_myStore_deleteProject parameters:requestInfo success:^(id responseObject) {
                        if (YJPPSUCCESS(responseObject)) {
                            [self.listArr removeObject:infoDic];
                        }else{
                            
                        }
                        [self.selfTableView reloadData];
                    } failure:^(NSError *error) {
                        
                    } showLoading:nil];
                }]];
                //弹出提示框；
                [self presentViewController:alert animated:true completion:nil];
            }
        };
        return itemCell;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 44;
    }else{
        NSDictionary * infoDic = self.listArr[indexPath.row -1];
        return [tableView fd_heightForCellWithIdentifier:@"lineActionItemCell" cacheByIndexPath:indexPath configuration:^(LineActionItemCell *cell) {
            cell.nameLabel.text = infoDic[@"project"];
        }];
    }
    return 44;
}
#pragma mark -
#pragma mark - **************** 请求我的店铺服务列表
-(void)requestServerList
{
    [self.listArr removeAllObjects];
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    [PPNetworkHelper POST:app_person_myStore_serviceProjects parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDic in responseObject[@"data"]) {
                [self.listArr addObject:infoDic];
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestServerList];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
